package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.KnowledgeBaseDao;
import cn.topevery.garden.entity.po.KnowledgeBase;
import cn.topevery.garden.entity.query.KnowledgeBaseQuery;
import cn.topevery.garden.entity.vo.KnowledgeBaseVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 知识库
 *
 * @author code-generate
 * @date 2022-10-28
 */
@Repository
public class KnowledgeBaseDaoImpl extends BaseDaoImpl<KnowledgeBase> implements KnowledgeBaseDao {
    @Override
    public <P extends DbCsp> List<KnowledgeBase> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<KnowledgeBaseVO> listPage(KnowledgeBaseQuery query) {
        String table = "t_knowledge_base tkb left join t_sys_dict tsd on tsd.c_dict_id=tkb.c_class_id";

        String columns = "tkb.*, tsd.c_dict_name className";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tkb.c_id desc");
        sqlQueryMate
                .where(true, "tkb.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getClassId()), "tkb.c_class_id", ScriptConditionEnum.Eq, query.getClassId())
                .where(StringUtils.isNotBlank(query.getName()), "tkb.c_name", ScriptConditionEnum.Like, query.getName())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, KnowledgeBaseVO.class);
    }

    @Override
    public KnowledgeBase getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }
}
